#include "stdafx.h"
#include "ThreadSafeMap.h"





void ThreadSafeMapTest()
{

    class ThreadSafeMapTester
    {
    public:
        void Run()
        {
            const int WRITERS_THREAD_COUNT = 2;
            for (int i = 0; i != WRITERS_THREAD_COUNT; ++i)
            {
                readersThread_.create_thread(boost::bind(&ThreadSafeMapTester::ReaderThread, this));
            }

            for (int i = 0; i != WRITERS_THREAD_COUNT; ++i)
            {
                writersThread_.create_thread(boost::bind(&ThreadSafeMapTester::WriterThread, this, i));
            }

            readersThread_.join_all();
            writersThread_.join_all();
            readersThread_.join_all();
            readersThread_.join_all();
        }

    public:
        void ReaderThread()
        {

            ::Sleep(20);
            size_t size = thradSafeMap_.size();
            std::cout<<size<<std::endl;
        } 

        void WriterThread(int nThread)
        {
            thradSafeMap_.set(nThread, nThread);
            thradSafeMap_.set(++nThread, nThread);
        }

    private:

        ThreadSafeMap<int, int> thradSafeMap_;
        boost::thread_group writersThread_;
        boost::thread_group readersThread_;

    };

    ThreadSafeMapTester test;
    test.Run();

}  









